/**
 * Project Name:bfs_v1.0.1
 * File Name:UserPntenterServiceImpl.java
 * Package Name:com.zeei.bfs.service.impl
 * Date:2016年12月19日上午11:15:46
 * Copyright (c) 2016, ZEEI All Rights Reserved. *
 *
 * ClassName:UserPntenterServiceImpl 
 * Function: TODO ADD FUNCTION.  
 * @author   zws 
 * @since    JDK 1.8	 
 */

package com.zeei.bfs.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zeei.bfs.dao.UserDao;
import com.zeei.bfs.dao.UserPntenterDao;
import com.zeei.bfs.service.UserPntenterService;
import com.zeei.common.constant.GlobalConstant;
import com.zeei.common.vo.UserPntenterVO;
import com.zeei.common.vo.UserVO;

@Service("userPntenterService")
public class UserPntenterServiceImpl implements UserPntenterService {

	@Autowired
	private UserPntenterDao userPntenterDao;
	
	@Autowired
	private UserDao userDao;

	@Override
	public boolean saveUserPntenter(Integer userId, List<String> objIds, Integer createId, String type) {
		if (userId == null) {
			return GlobalConstant.FAILED;
		}
		if (objIds == null) {
			userPntenterDao.deleteUserPntenterByUserId(userId);
			return GlobalConstant.SUCCEED;
		}
		userPntenterDao.deleteUserPntenterByUserId(userId);
		List<UserPntenterVO> userPntenterList = new ArrayList<UserPntenterVO>();
		for (int i = 0; i < objIds.size(); i++) {
			UserPntenterVO userPntenterVO = new UserPntenterVO();
			userPntenterVO.setUserId(userId);
			userPntenterVO.setObjId(Integer.valueOf(objIds.get(i)));
			userPntenterVO.setCreateId(createId);
			userPntenterVO.setType(type);
			userPntenterList.add(userPntenterVO);
		}
		if (CollectionUtils.isNotEmpty(userPntenterList)) {
			int N = GlobalConstant.DEFAULT_BATCH_SIZE;
			int size = userPntenterList.size();
			int index = 0;
			int toIndex = N;
			while (toIndex < size) {
				userPntenterDao.insertUserPntenterList(userPntenterList.subList(index, toIndex));
				toIndex += N;
				index += N;
			}
			userPntenterDao.insertUserPntenterList(userPntenterList.subList(index, size));
		}
		return GlobalConstant.SUCCEED;
	}

	@Override
	public boolean addUserPntenter(Integer userId, Integer objId, Integer createId, String type) {
		insertAdminPntenter(userId,objId, createId, type);
		boolean successState = insertUserPntenter(userId, objId, createId, type);
		return successState;
	}
	
	private void insertAdminPntenter(Integer userId,Integer objId, Integer createId, String type) {
		UserVO queryUser=new UserVO();
		queryUser.setLoginname(GlobalConstant.ADMIN_NAME);
		List<UserVO> userList = userDao.queryUserByLoginName(queryUser);
		if(isOnlyOneCollection(userList)){
			UserVO adminUser = userList.get(0);
			Integer adminUserId = adminUser.getUserid();
			if (!adminUserId.equals(userId)) {
				insertUserPntenter(adminUser.getUserid(), objId, createId, type);
			}
		}
	}

	private boolean insertUserPntenter(Integer userId, Integer objId, Integer createId, String type) {
		Map<String, Object> condition = new HashMap<String, Object>();
		condition.put("objId", objId);
		condition.put("type", type);
		condition.put("userId", userId);
		List<UserPntenterVO> userPntenterList = userPntenterDao.queryUserPntenterByCondition(condition);
		boolean successState=true;
		if (!isOnlyOneCollection(userPntenterList)) {//不存在
			UserPntenterVO userPntenter=new UserPntenterVO();
			userPntenter.setObjId(objId);
			userPntenter.setUserId(userId);
			userPntenter.setType(type);
			userPntenter.setCreateId(createId);
			List<UserPntenterVO> insertList=new ArrayList<UserPntenterVO>(); 
			insertList.add(userPntenter);
			Integer count = userPntenterDao.insertUserPntenterList(insertList);
			successState=count>0;
		}	
		return successState;
	}

	/**
	 * 方法描述:是只有一个数据的集合吗
	 *
	 * @param collection
	 * @return
	 */
	private static <T> boolean isOnlyOneCollection(Collection<T> collection){
		return collection!=null && collection.size()==1;
	}

	@Override
	public List<UserPntenterVO> queryUserPntenter(Integer userId, String type, UserVO currentUser) {
		List<UserPntenterVO> resultList;
		if (GlobalConstant.ADMIN_NAME.equalsIgnoreCase(currentUser.getLoginname())) {
			Map<String, Object> condition = new HashMap<String, Object>();
			condition.put("userId", userId);
			condition.put("type", type);
			resultList = userPntenterDao.queryUserPntenterByCondition(condition);
		} else {
			Map<String, Object> condition = new HashMap<String, Object>();
			condition.put("userId", userId);
			condition.put("type", type);
			condition.put("selfUserId", currentUser.getUserid());
			resultList = userPntenterDao.queryUserPntenterByCondition(condition);
		}
		return resultList;
	}

}
